package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.QueryObject.StrategyThemeQuery;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.respository.DestinationRespository;
import cn.wolfcode.luowowo.respository.StrategyThemeRespository;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
//@Transactional  暂时不管事务
public class StrategyThemeServiceImpl implements IStrategyThemeService {

    @Autowired
    private StrategyThemeRespository strategyThemerRespository;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private DestinationRespository destinationRespository;


    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public StrategyTheme get(String id) {
        Optional<StrategyTheme> optional = strategyThemerRespository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    @Override
    public List<StrategyTheme> list() {
        return strategyThemerRespository.findAll();
    }

    @Override
    //Page对象里面包括 : 总页数 每页显示条数
    //使用mongoTemplate 只能返回Page 需要把list封装成 page对象再返回
    public Page<StrategyTheme> query(StrategyThemeQuery qo) {
        Query query = new Query();
        PageRequest page = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "sequence");
        return DBHelper.query(mongoTemplate, StrategyTheme.class, query, page);
    }

    @Override
    public void saveOrUpdate(StrategyTheme strategyTheme) {
        if (!StringUtils.hasLength(strategyTheme.getId())) {
            //添加
            strategyTheme.setId(null);
            strategyThemerRespository.save(strategyTheme);
        } else {
            //更新
            //方案一: 1.查询 2.替代 3.save
            //方案二:

            //条件
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(strategyTheme.getId()));
            //更新的列
            Update update = new Update();
            update.set("name", strategyTheme.getName());
            update.set("status", strategyTheme.getState());
            update.set("sequence", strategyTheme.getSequence());
            mongoTemplate.updateMulti(query, update, StrategyTheme.class);
        }
    }

    @Override
    public void delete(String id) {
        strategyThemerRespository.deleteById(id);
    }


}
